package com.huahua.bigdata.spark.rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

import java.util.Arrays;
import java.util.List;

public class Spark05_Operate_groupBy {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        List<Integer> datas = Arrays.asList(1, 2, 3, 4);

        JavaRDD<Integer> rdd = jsc.parallelize(datas);

        // TODO RDD的方法: groupBy, 按照指定的规则对数据进行分组
//        JavaPairRDD<Object, Iterable<Integer>> groupByRDD = rdd.groupBy(new Function<Integer, Object>() {
//            @Override
//            public Object call(Integer v1) throws Exception {
//                return "a";
//            }
//        });

        JavaPairRDD<Boolean, Iterable<Integer>> groupByRDD = rdd.groupBy(num -> num % 2 == 0);

        groupByRDD.collect().forEach(System.out::println);
        jsc.close();


    }
}
